package com.zonda.myjacksonstudy;

import android.graphics.Point;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.zonda.myjacksonstudy.model.ArticleModel;
import com.zonda.myjacksonstudy.model.OpenActionModel;
import com.zonda.myjacksonstudy.model.ad.AdAnimateModel;
import com.zonda.myjacksonstudy.model.ad.AdAnimateResult;
import com.zonda.myjacksonstudy.model.ad.AdAnimateType;
import com.zonda.myjacksonstudy.model.ad.AnimateType;
import com.zonda.myjacksonstudy.model.ad.ChildAnimateModel;

import java.util.ArrayList;

public class FastJsonTest {

  public static class EnumFilter implements ValueFilter{

    @Override
    public Object process(Object o, String s, Object o1) {

      if(o1 instanceof AnimateType){
        return ((AnimateType) o1).typeName;
      }

      if(o1 instanceof AdAnimateType){
        return ((AdAnimateType) o1).jsonValueName;
      }
      return o1;
    }
  }

  public static AdAnimateResult createTest1(){

    AdAnimateResult adAnimateResult = new AdAnimateResult();
    adAnimateResult.setBackgroundWidth(1242);
    adAnimateResult.setBackgroundHeight(930);
    adAnimateResult.setBackgroundUrl("<this is local path uri>");

    ArrayList<AdAnimateModel> adAnimates = new ArrayList<>();

    AdAnimateModel adAnimateModel = new AdAnimateModel();
    adAnimateModel.setId("translate01");
    adAnimateModel.setAdAnimateType(AdAnimateType.TWEEN_PARALLAX);
    ChildAnimateModel parallaxAnimate = new ChildAnimateModel();
    parallaxAnimate.setId("translate01-parallax01");
    parallaxAnimate.setUrl("<this is local path uri>");
    parallaxAnimate.setAnimateType(AnimateType.TRANSLATE);
    parallaxAnimate.setStartPointX(-880);
    parallaxAnimate.setStartPointY(691);
    parallaxAnimate.setEndPointX(247);
    parallaxAnimate.setEndPointY(344);
    parallaxAnimate.setWidth(880);
    parallaxAnimate.setHeight(508);
    adAnimateModel.setAdParallaxAnimate(parallaxAnimate);
    adAnimates.add(adAnimateModel);

    adAnimateModel = new AdAnimateModel();
    adAnimateModel.setId("translate02");
    adAnimateModel.setAdAnimateType(AdAnimateType.TWEEN_PARALLAX);
    parallaxAnimate = new ChildAnimateModel();
    parallaxAnimate.setId("translate02-parallax02");
    parallaxAnimate.setUrl("<this is local path uri>");
    parallaxAnimate.setAnimateType(AnimateType.TRANSLATE);
    parallaxAnimate.setStartPointX(1414);
    parallaxAnimate.setStartPointY(-122);
    parallaxAnimate.setEndPointX(115);
    parallaxAnimate.setEndPointY(141);
    parallaxAnimate.setWidth(708);
    parallaxAnimate.setHeight(289);
    adAnimateModel.setAdParallaxAnimate(parallaxAnimate);
    adAnimates.add(adAnimateModel);

    adAnimateModel = new AdAnimateModel();
    adAnimateModel.setId("translate03");
    adAnimateModel.setAdAnimateType(AdAnimateType.TWEEN_PARALLAX);
    parallaxAnimate = new ChildAnimateModel();
    parallaxAnimate.setId("translate03-parallax03");
    parallaxAnimate.setUrl("<this is local path uri>");
    parallaxAnimate.setAnimateType(AnimateType.TRANSLATE);
    parallaxAnimate.setStartPointX(-1410);
    parallaxAnimate.setStartPointY(353);
    parallaxAnimate.setEndPointX(59);
    parallaxAnimate.setEndPointY(59);
    parallaxAnimate.setWidth(813);
    parallaxAnimate.setHeight(219);
    adAnimateModel.setAdParallaxAnimate(parallaxAnimate);
    adAnimates.add(adAnimateModel);

    adAnimateModel = new AdAnimateModel();
    adAnimateModel.setId("alpha01");
    adAnimateModel.setAdAnimateType(AdAnimateType.TWEEN_PARALLAX);
    parallaxAnimate = new ChildAnimateModel();
    parallaxAnimate.setId("alpha01-parallax01");
    parallaxAnimate.setUrl("<this is local path uri>");
    parallaxAnimate.setAnimateType(AnimateType.ALPHA);
    parallaxAnimate.setStartPointX(902);
    parallaxAnimate.setStartPointY(812);
    parallaxAnimate.setWidth(235);
    parallaxAnimate.setHeight(50);
    parallaxAnimate.setStartAlpha(0);
    parallaxAnimate.setEndAlpha(255);
    adAnimateModel.setAdParallaxAnimate(parallaxAnimate);
    adAnimates.add(adAnimateModel);

    adAnimateModel = new AdAnimateModel();
    adAnimateModel.setId("frames01");
    adAnimateModel.setAdAnimateType(AdAnimateType.FRAMES_PARALLAX);
    ArrayList<ChildAnimateModel> adFrameAnimates = new ArrayList<>();
    for (int i = 0; i < 3 ;i++){
      ChildAnimateModel adFrameAnimate = new ChildAnimateModel();
      adFrameAnimate.setId("frame0" + i);
      adFrameAnimate.setUrl("<this is local path uri>");
      adFrameAnimate.setAnimateType(AnimateType.TRANSLATE);
      adFrameAnimate.setStartPointX(-407);
      adFrameAnimate.setStartPointY(908);
      adFrameAnimate.setEndPointX(720);
      adFrameAnimate.setEndPointY(561);
      adFrameAnimate.setWidth(388);
      adFrameAnimate.setHeight(199);
      adFrameAnimates.add(adFrameAnimate);
    }
    adAnimateModel.setAdFrameAnimates(adFrameAnimates);
    adAnimates.add(adAnimateModel);

    adAnimateModel = new AdAnimateModel();
    adAnimateModel.setId("property01");
    adAnimateModel.setAdAnimateType(AdAnimateType.PROPERTY_ANIMATION);
    ChildAnimateModel propertyAnimate = new ChildAnimateModel();
    propertyAnimate.setId("property01-scale01");
    propertyAnimate.setUrl("<this is local path uri>");
    propertyAnimate.setWidth(60);
    propertyAnimate.setHeight(60);
    propertyAnimate.setAnimateType(AnimateType.SCALE);
    propertyAnimate.setStartScale("0.1");
    propertyAnimate.setEndScale("1.1");
    propertyAnimate.setDuration(500);
    adAnimateModel.setAdPropertyAnimate(propertyAnimate);
    adAnimates.add(adAnimateModel);

    adAnimateResult.setAdAnimates(adAnimates);

    return adAnimateResult;
  }

  public static AdAnimateResult createTest2(){

    AdAnimateResult adAnimateResult = new AdAnimateResult();

    ArrayList<AdAnimateModel> adAnimateModels = new ArrayList<>();
    AdAnimateModel adAnimateModel = new AdAnimateModel();
    ChildAnimateModel adParallaxAnimate = new ChildAnimateModel();
    adAnimateModel.setAdParallaxAnimate(adParallaxAnimate);
    ChildAnimateModel propertyAnimate = new ChildAnimateModel();
    adAnimateModel.setAdPropertyAnimate(propertyAnimate);
    ArrayList<ChildAnimateModel> adFrameAnimates = new ArrayList<>();
    ChildAnimateModel adFrameAnimate = new ChildAnimateModel();
    adFrameAnimates.add(adFrameAnimate);
    adAnimateModel.setAdFrameAnimates(adFrameAnimates);
    adAnimateModels.add(adAnimateModel);
    adAnimateResult.setAdAnimates(adAnimateModels);

    return adAnimateResult;
  }

  public static void testAdAnimateToJsonString() {

    AdAnimateResult animate = createTest2();
    SerializerFeature[] features = {SerializerFeature.PrettyFormat,
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteNullStringAsEmpty
    };

    EnumFilter filter = new EnumFilter();

    String parallaxAnimateJsonString = JSON.toJSONString(animate, filter, features);

//    ParallaxAnimateSerializeFilter filter = new ParallaxAnimateSerializeFilter();
//
//    String parallaxAnimateJsonString = JSON.toJSONString(parallaxAnimateModel, filter, features);

    Log.d(Contants.LOG_TAG, "testAdAnimateToJsonString: " + parallaxAnimateJsonString);
  }

  public static void testToJsonString() {

    ArticleModel articleModel = new ArticleModel();
    articleModel.setAuthorName("哈哈哈");
    articleModel.setPk("999");
    articleModel.setTitle("Fastjson full support databind, it's simple to use.");
    OpenActionModel openActionModel = new OpenActionModel();
    ArticleModel openArticle = new ArticleModel();
    openArticle.setAuthorName("好好好");
    openArticle.setPk("9994444");
    openArticle.setTitle("使用同一项目为您");
    openActionModel.setArticle(openArticle);
    openActionModel.setOnlySet("onlySetValue");
    articleModel.setOpenInfo(openActionModel);

    SerializerFeature[] features = {//SerializerFeature.PrettyFormat,
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteNullStringAsEmpty};
    String articleJsonString = JSON.toJSONString(articleModel, features);

    Log.d(Contants.LOG_TAG, "articleJsonString: " + articleJsonString);
  }

  public static void testToObj() {

    String testJsonString = "{\"author_name\":\"哈哈哈\",\"open_info\":{\"article\":{\"author_name\"" +
            ":\"好好好\",\"open_info\":null,\"pk\":\"9994444\",\"title\":\"使用同一项目为您\"},\"id\":\"\"" +
            ",\"onlyOfPublic\":\"\",\"only_get\":\"onlyGetValue\",\"only_set\":\"onlySetValue\",\"test_web\":null,\"type\":\"\"},\"pk\"" +
            ":\"999\",\"title\":\"Fastjson full support databind, it's simple to use.\"}";

    Feature[] features = {
            //Feature.AllowComment
    };
    ArticleModel testArticleModel = JSON.parseObject(testJsonString, ArticleModel.class, features);

    Log.d(Contants.LOG_TAG, "testArticleModel: " + testArticleModel.toString());
  }
}
